﻿Imports System.IO.Ports

Public Class Device2SerialBridge
    Implements I_HardwareDataExchange
    Private m_SerialPort As System.IO.Ports.SerialPort = Nothing
    Public Event PortOpenStatus(ByVal Status As Boolean)
    Private m_isOpen As Boolean = False
    Public Property SerialIOPort() As SerialPort
        Get
            Return m_SerialPort
        End Get
        Set(ByVal value As SerialPort)
            m_SerialPort = value
        End Set
    End Property
    Public ReadOnly Property BytesToRead() As Integer Implements I_HardwareDataExchange.BytesToRead
        Get
            Try
                If m_SerialPort IsNot Nothing And MyBase.DesignMode = False Then
                    Return m_SerialPort.BytesToRead
                Else
                    Return 0
                End If
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Device to Serial Bridge - bytes2read")
            End Try
        End Get
    End Property

    Public ReadOnly Property BytesToWrite() As Integer Implements I_HardwareDataExchange.BytesToWrite

        Get
            Try
                If m_SerialPort IsNot Nothing And MyBase.DesignMode = False Then
                    Return m_SerialPort.BytesToWrite
                Else
                    Return 0
                End If
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Device to Serial Bridge - bytes2write")
            End Try
        End Get
    End Property

    Public Sub ClearBuffers() Implements I_HardwareDataExchange.ClearBuffers
        Try
            If m_SerialPort IsNot Nothing And MyBase.DesignMode = False Then
                If m_SerialPort.IsOpen = False Then m_SerialPort.Open()
                m_SerialPort.DiscardInBuffer()
                m_SerialPort.DiscardOutBuffer()

            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Device to Serial Bridge - clearbuff")
        End Try
    End Sub

    Public Function Read() As Byte() Implements I_HardwareDataExchange.Read
        Try
            If m_SerialPort IsNot Nothing And MyBase.DesignMode = False Then
                If m_SerialPort.IsOpen = False Then m_SerialPort.Open()
                Dim D(m_SerialPort.BytesToRead) As Byte
                m_SerialPort.Read(D, 0, m_SerialPort.BytesToRead)
                Return D
            End If
            Return Nothing
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Device to Serial Bridge - read")
            Return Nothing
        End Try
    End Function

    Public Sub Write(ByVal data() As Byte) Implements I_HardwareDataExchange.Write
        Try
            If m_SerialPort IsNot Nothing And MyBase.DesignMode = False Then
                If m_SerialPort.IsOpen = False Then m_SerialPort.Open()
                m_SerialPort.Write(data, 0, data.Length)
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Device to Serial Bridge - write")
        End Try

    End Sub

    Private Sub SerialBridge_Disposed(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            If m_SerialPort IsNot Nothing And MyBase.DesignMode = False Then
                m_SerialPort.Close()
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Device to Serial Bridge")
        End Try
    End Sub

    Public Function Read(ByVal Count As Integer) As Byte() Implements I_HardwareDataExchange.Read
        Try
            If m_SerialPort IsNot Nothing And MyBase.DesignMode = False Then
                If m_SerialPort.IsOpen = False Then m_SerialPort.Open()
                Dim D(Count - 1) As Byte
                m_SerialPort.Read(D, 0, Count)
                Return D
            End If
            Return Nothing
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Device to Serial Bridge - read")
            Return Nothing
        End Try

    End Function

    Public Sub ClosePort() Implements I_HardwareDataExchange.ClosePort
        Try
            If m_SerialPort IsNot Nothing And MyBase.DesignMode = False Then
                m_SerialPort.Close()
                m_isOpen = False
                RaiseEvent PortOpenStatus(m_isOpen)
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Device to Serial Bridge - closeport")
        End Try
    End Sub
    Public ReadOnly Property IsOpen() As Boolean Implements I_HardwareDataExchange.IsOpen
        Get
            Return m_isOpen
        End Get
    End Property

    Public Sub OpenPort() Implements I_HardwareDataExchange.OpenPort
        Try
            If m_SerialPort IsNot Nothing And MyBase.DesignMode = False Then
                If m_SerialPort.IsOpen = False Then
                    m_SerialPort.Open()
                End If
                Do
                    Windows.Forms.Application.DoEvents()
                Loop Until m_SerialPort.IsOpen = True
                m_isOpen = True
                RaiseEvent PortOpenStatus(m_isOpen)
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Device to Serial Bridge - openport")
        End Try
    End Sub
End Class
